package zhoug.logger;

import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 日志打印工具
 */
public class Logger {
    private static final String TAG = ">>>Logger";
    public static final int RELEASE = 1;
    public static final int DEBUG = 5;
    private static int status = RELEASE;
    private static boolean bigLog = true;//开启后日志太长时分段显示
    private static int maxLength = 1000;//一条日志最多打印4000字节,即全中文时最多1000字符

    /**
     * 是否开启debug模式
     *
     * @param debug
     */
    public static void setDebug(boolean debug) {
        if (debug) {
            status = DEBUG;
        } else {
            status = RELEASE;
        }
    }

    public static void setMaxLength(int maxLength) {
        Logger.maxLength = maxLength;
    }

    public static void setBigLog(boolean bigLog) {
        Logger.bigLog = bigLog;
    }


    public static void i(String TAG, String msg) {
        if (status >= RELEASE) {
            if (msg == null) {
                Log.i(TAG, "null");
            } else {
                if (bigLog && msg.length() > maxLength) {
                    //长日志拆分打印
                    List<String> list = splitLog(msg);
                    for (int i = 0; i < list.size(); i++) {
                        Log.i(TAG + " logIndex" + i+" ", list.get(i));
                    }
                } else {
                    Log.i(TAG, msg);
                }
            }
        }
    }

    public static void d(String TAG, String msg) {
        if (status >= DEBUG) {
            if (msg == null) {
                Log.d(TAG, "null");
            } else {
                if (bigLog && msg.length() > maxLength) {
                    //长日志拆分打印
                    List<String> list = splitLog(msg);
                    for (int i = 0; i < list.size(); i++) {
                        Log.d(TAG + " logIndex" + i+" ", list.get(i));
                    }
                } else {
                    Log.d(TAG, msg);
                }
            }
        }
    }

    public static void e(String TAG, String msg) {
        if (status >= RELEASE) {
            if (msg == null) {
                Log.e(TAG, "null");
            } else {
                if (bigLog && msg.length() > maxLength) {
                    //长日志拆分打印
                    List<String> list = splitLog(msg);
                    for (int i = 0; i < list.size(); i++) {
                        Log.e(TAG + " logIndex" + i+" ", list.get(i));
                    }
                } else {
                    Log.e(TAG, msg);
                }
            }
        }
    }

    public static void w(String TAG, String msg) {
        if (status >= RELEASE) {
            if (msg == null) {
                Log.w(TAG, "null");
            } else {
                if (bigLog && msg.length() > maxLength) {
                    //长日志拆分打印
                    List<String> list = splitLog(msg);
                    for (int i = 0; i < list.size(); i++) {
                        Log.w(TAG + " logIndex" + i+" ", list.get(i));
                    }
                } else {
                    Log.w(TAG, msg);
                }
            }
        }
    }

    /**
     * 日志分段
     *
     * @param msg
     */
    private static List<String> splitLog( String msg) {
        //字符长度小于1000直接返回
        int length = msg.length();
        if (length <= maxLength) {
            return Collections.singletonList(msg);
        }
        List<String> logs = new ArrayList<>();
        int start = 0;
        int end = maxLength;
        while (start < length) {
//            Log.d("splitLog:", start + "-" + end);
            logs.add(msg.substring(start, end));
            start = end;
            end = Math.min(start + maxLength, length);
        }
        return logs;
    }


}
